package com.jdd.modules.area.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;

import com.jdd.common.system.query.QueryGenerator;
import com.jdd.common.system.util.R;
import com.jdd.common.util.DateUtil;
import com.jdd.common.util.MathUtil;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.common.util.excel.entity.params.ExportParams;
import com.jdd.common.util.excel.entity.params.ImportParams;
import com.jdd.common.util.excel.util.ExcelImportUtil;
import com.jdd.common.util.excel.web.entity.vo.NormalExcelConstants;
import com.jdd.common.util.excel.web.view.EasypoiSingleExcelView;

import com.jdd.modules.area.entity.ChargeRulesModule;
import com.jdd.modules.area.entity.ChargeRulesModuleStep;
import com.jdd.modules.area.service.IChargeRulesModuleService;
import com.jdd.modules.area.service.IChargeRulesModuleStepService;
import com.jdd.modules.area.vo.ChargeRulesModuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @Description: 停车场计费规则模块表
 * @Author: jdd
 * @Date:   2021-07-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags="停车场计费规则模块表")
@RestController
@RequestMapping("/parking/chargeRulesModule")
public class ChargeRulesModuleController {
    @Autowired
    private IChargeRulesModuleService chargeRulesModuleService;
    @Resource
    private IChargeRulesModuleStepService chargeRulesModuleStepService;

    /**
     * 分页列表查询
     * @param chargeRulesModule
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-分页列表查询")
    @ApiOperation(value="停车场计费规则模块表-分页列表查询", notes="停车场计费规则模块表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ChargeRulesModule>> queryPageList(ChargeRulesModule chargeRulesModule,
                                                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                          HttpServletRequest req) {
        Result<IPage<ChargeRulesModule>> result = new Result<IPage<ChargeRulesModule>>();
        QueryWrapper<ChargeRulesModule> queryWrapper = QueryGenerator.initQueryWrapper(chargeRulesModule, req.getParameterMap());
        Page<ChargeRulesModule> page = new Page<ChargeRulesModule>(pageNo, pageSize);
        IPage<ChargeRulesModule> pageList = chargeRulesModuleService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    /**
     * 根据计费规则id，获取对应计费规则模块
     *
     * */
    @AutoLog(value = "停车场计费规则模块表-获取规则模块")
    @ApiOperation(value ="停车场计费规则模块表-获取规则模块",notes = "停车场计费规则模块表-获取规则模块")
    @PostMapping(value = "getChargeRulesModules")
    public Result<List<ChargeRulesModule>> getChargeRulesModules(@RequestBody ChargeRulesModule chargeRulesModule)
    {
        Result<List<ChargeRulesModule>> result = new Result<>();
        List<ChargeRulesModule> list = chargeRulesModuleService.getChargeRulesModule(chargeRulesModule.getChargeRulesId());
        result.setResult(list);
        return result;
    }
    /**
     * 根据计费规则模块id,获取阶梯计费模块
     * */
    @AutoLog(value = "停车场计费规则模块表-获取阶梯计费规则模块")
    @ApiOperation(value ="停车场计费规则模块表-获取阶梯计费规则模块",notes = "停车场计费规则模块表-获取阶梯计费规则模块")
    @PostMapping(value = "getChargeSteps")
    public Result<List<ChargeRulesModuleStep>> getChargeSteps(@RequestBody ChargeRulesModule chargeRulesModule)
    {
        Result<List<ChargeRulesModuleStep>> result = new Result<>();
        List<ChargeRulesModuleStep> list = chargeRulesModuleStepService.getChargeRulesModuleSteps(chargeRulesModule.getId());
        result.setResult(list);
        return result;
    }

    /**
     * 添加
     * @param chargeRulesModule
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-添加")
    @ApiOperation(value="停车场计费规则模块表-添加", notes="停车场计费规则模块表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ChargeRulesModuleVO chargeRulesModuleVO, HttpServletRequest req) {
        Result<String> result = new Result<String>();
        try {
            result.setResult(chargeRulesModuleService.add(chargeRulesModuleVO));
        }catch (Exception e)
        {
            log.error("{}",e);
            return Result.error("操作失败:"+e.getMessage());
        }
        return result;
    }
    /**
     *  编辑
     * @param chargeRulesModule
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-编辑")
    @ApiOperation(value="停车场计费规则模块表-编辑", notes="停车场计费规则模块表-编辑")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody ChargeRulesModuleVO chargeRulesModuleVO, HttpServletRequest req)  {
        Result<String> result = new Result<String>();
        try {
            result.setResult(chargeRulesModuleService.edit(chargeRulesModuleVO));
        }catch (Exception e)
        {
            log.error("{}",e);
            return Result.error("操作失败:"+e.getMessage());
        }
        return result;
    }

    /**
     *   通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-通过id删除")
    @ApiOperation(value="停车场计费规则模块表-通过id删除", notes="停车场计费规则模块表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        Result<String> result = new Result<String>();
        try {
            result.setResult(chargeRulesModuleService.delete(id));
        }catch (Exception e)
        {
            log.error("{}",e);
            return Result.error("操作失败:"+e.getMessage());
        }
        return result;
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-批量删除")
    @ApiOperation(value="停车场计费规则模块表-批量删除", notes="停车场计费规则模块表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<ChargeRulesModule> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        Result<ChargeRulesModule> result = new Result<ChargeRulesModule>();
        if(ids==null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        }else {
            this.chargeRulesModuleService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @AutoLog(value = "停车场计费规则模块表-通过id查询")
    @ApiOperation(value="停车场计费规则模块表-通过id查询", notes="停车场计费规则模块表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ChargeRulesModule> queryById(@RequestParam(name="id",required=true) String id) {
        Result<ChargeRulesModule> result = new Result<ChargeRulesModule>();
        ChargeRulesModule chargeRulesModule = chargeRulesModuleService.getById(id);
        if(chargeRulesModule==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(chargeRulesModule);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response,ChargeRulesModule chargeRulesModule) {
        // Step.1 组装查询条件
        QueryWrapper<ChargeRulesModule> queryWrapper = QueryGenerator.initQueryWrapper(chargeRulesModule, request.getParameterMap());

        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new EasypoiSingleExcelView());
        List<ChargeRulesModule> pageList = chargeRulesModuleService.list(queryWrapper);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "停车场计费规则模块表列表");
        mv.addObject(NormalExcelConstants.CLASS, ChargeRulesModule.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("停车场计费规则模块表列表数据", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }


}
